package com.jcnet.bizaia;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import cn.sharesdk.framework.ShareSDK;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.jcnet.bizaia.adapter.MainMenuAdapter;
import com.jcnet.bizaia.fragment.DiscussionInfoFragment;
import com.jcnet.bizaia.fragment.VodInfoFragment;
import com.jcnet.bizaia.fragment.VoiceInfoFragment;
import com.jcnet.bizaia.view.HeadViewPageView;
import com.mozillaonline.providers.downloads.ui.DownloadList;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingLeftInAnimationAdapter;
import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.LinePageIndicator;
import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.UnderlinePageIndicator;

public class MainActivity  extends SherlockFragmentActivity implements OnClickListener,OnItemClickListener{

	public static final int DISCUSS_ID = 1;
	public static final int VOD_ID = 2;
	public static final int VOICE_ID = 3;

	private TabPageIndicator indicator;

	private ViewPager viewPager;

	private String [] mainTitles; 


	private ListView menuListView;

	private boolean isShowMenu = false;


	private int pageNow = 0;

	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		context = this;
		mainTitles = getResources().getStringArray(R.array.main_category);

		initActionBars();

		initView();

		sendBroadcast(new Intent(DiscussionInfoFragment.DISCUSSIONINFOFRAGMENT_STRING));
		
		
	}

	private void initActionBars() {
		// TODO Auto-generated method stub
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setLogo(getResources().getDrawable(R.drawable.logo));
		//actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bg));
		actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.mainActionBg));
		actionBar.show();
	}



	@SuppressLint("NewApi")
	private void initView() {
		// TODO Auto-generated method stub

		menuListView = (ListView)findViewById(R.id.mainMenuList);

		SwingBottomInAnimationAdapter swingLeftInAnimationAdapter = new SwingBottomInAnimationAdapter(new SwingLeftInAnimationAdapter(new MainMenuAdapter(this)));
		swingLeftInAnimationAdapter.setInitialDelayMillis(App.listAnimTime);
		swingLeftInAnimationAdapter.setAbsListView(menuListView);
		menuListView.setAdapter(swingLeftInAnimationAdapter);

		menuListView.setOnItemClickListener(this);


		indicator = (TabPageIndicator)findViewById(R.id.indicator);
		viewPager = (ViewPager)findViewById(R.id.pager);
		viewPager.setAdapter(new GoogleMusicAdapter(getSupportFragmentManager()));
		viewPager.setOffscreenPageLimit(mainTitles.length);
		indicator.setViewPager(viewPager);


		

		indicator.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub

				pageNow = arg0;

				/*indicator.setBackgroundResource(R.drawable.custom_tab_indicator);
				indicator.setBackground(getResources().getDrawable(R.drawable.custom_tab_indicator));
				 */
				switch (arg0) {
				case 0:
					//indicator.setFooterColor(getResources().getColor(R.color.mainDis));
					break;
				case 1:
					//indicator.setFooterColor(getResources().getColor(R.color.mainVod));
					break;
				case 2:
					//indicator.setFooterColor(getResources().getColor(R.color.mainVoice));

					break;

				default:
					break;
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});



	}




	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		// TODO Auto-generated method stub
		getSupportMenuInflater().inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}


	class GoogleMusicAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
		public GoogleMusicAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {

			//return fragments.get(position);
			if(position==0){
				return DiscussionInfoFragment.newInstance(0);
			}
			if(position==1){
				return VodInfoFragment.newInstance(1);
			}
			else{
				return VoiceInfoFragment.newInstance(2);
			}

			//return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return mainTitles[position];
		}

		@Override public int getIconResId(int index) {
			return 0;
		}

		@Override
		public int getCount() {
			return mainTitles.length;
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {

		case R.id.menu_user:

			if(isShowMenu){

				menuListView.setVisibility(View.GONE);
			}else{
				menuListView.setVisibility(View.VISIBLE);
			}
			isShowMenu = !isShowMenu;
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.searchButton:

			switch (pageNow) {
			case 0:
				startActivity(new Intent(MainActivity.this, DiscussSearchActivity.class));
				break;
			case 1:
				startActivity(new Intent(MainActivity.this, VodSearchActivity.class));
				break;
			case 2:
				startActivity(new Intent(MainActivity.this, VoiceSearchActivity.class));

				break;

			default:
				break;
			}

			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		switch (arg2) {
		case 0:
			startActivity(new Intent(context, FavouriteActivity.class));
			break;
		case 1:
			startActivity(new Intent(context, SettingUserMenuActivity.class));

			break;
		case 3:
			Intent intent = new Intent();
			//intent.setClass(context, DownloadList.class);
			//intent.setClass(context, JCDownloadList.class);
			intent.setClass(context, MyJCDownloadActivity.class);
			startActivity(intent);
			break;
		case 4:
			startActivity(new Intent(context, SettingAcivity.class));
			break;
		case 2:
			startActivity(new Intent(context, SettingTalkActivity.class));
			break;
		default:
			break;
		}
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
			alertDialog.setTitle(getString(R.string.exitApp));
			alertDialog.setPositiveButton(getString(R.string.ok),
					new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					alertDialog.create().dismiss();
					finish();
					ShareSDK.stopSDK(MainActivity.this);
					android.os.Process.killProcess(android.os.Process.myPid());
					System.exit(0);
				}
			});
			alertDialog.setNeutralButton(getString(R.string.cancle),
					new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					alertDialog.create().dismiss();
				}
			});
			alertDialog.show();
			return true;
		}else{
			return super.onKeyDown(keyCode, event);
		}
	}


}
